<?php

declare (strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Model;

use Hyperf\Database\Model\SoftDeletes;
use Qbhy\HyperfAuth\Authenticatable;

class Manager extends Model implements Authenticatable
{
    use SoftDeletes;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected ?string $table = 'manager';
    protected array $hidden = ['password'];

    protected array $casts = ['role_id' => 'integer'];
    public static $statusList = [1 => '启用', -1 => '禁用'];

    /**
     * 角色组
     * @return \Hyperf\Database\Model\Relations\BelongsTo
     */
    public function role()
    {
        return $this->belongsTo(Role::class, 'role_id');
    }
    //增加 jwt接口需要的方法
//    public function getJwtCustomClaims() : array
//    {
//        return ['guard' => 'manager'];
//    }
//    public function getJwtIdentifier()
//    {
//        return $this->getKey();
//    }
    public function getId()
    {
        return $this->id;
    }

    public static function retrieveById($key): ?Authenticatable
    {
        return self::query()->find($key);
    }

    public function getStatusTextAttribute()
    {
        return static::$statusList[$this->status] ?? '禁用';
    }

    /**
     * 通过用户名查找
     * @param $username
     * @return \Hyperf\Database\Model\Builder|\Hyperf\Database\Model\Model|object|null
     */
    public static function getByUsername($username)
    {
        return static::query()->where('username', $username)->first();
    }
    /**
     * 设置密码
     * @param $password
     * @return $this
     */
//    public function setPasswordAttribute($password)
//    {
//        $this->attributes['password'] = Hash::make($password);
//        return $this;
//    }
    /**
     * 权限检查
     * @param $rights
     * @return bool
     */
    public function checkRights($rights)
    {
        /** @var Role $role */
        $role = $this->role;
        if (!$role) {
            return true;
        }
        $rightsArray = $role->rights;
        if ($rightsArray) {
            $rightsArray = explode(',', $rightsArray);
        } else {
            $rightsArray = [];
        }

        if (!in_array($rights, $rightsArray)) {
            return false;
        }
        return true;
    }

    /**
     * 启用/禁用
     * @return $this
     */
    public function handle()
    {
        if ($this->status == 1) {
            $this->status = -1;
        } else {
            $this->status = 1;
        }
        return $this;
    }

    public function scopeKeyword($q, $keyword)
    {
        return $q->where(function ($q) use ($keyword) {
            $q->where('name', 'like', '%' . $keyword . '%');
            $q->orWhere('username', 'like', '%' . $keyword . '%');
            $q->orWhere('mobile', 'like', '%' . $keyword . '%');
            $q->orWhere('email', 'like', '%' . $keyword . '%');
        });
    }

    /**
     * 检查密码
     * @param $password
     * @return bool
     */
    public function checkPwd($password)
    {
        return $password == $this->password;
//        return Hash::check($password, $this->password);
    }

    /**
     * 接口返回
     * @return array
     */
    public function toApi()
    {
        return [
            'id' => $this->id,
            'username' => $this->username ?? '',
            'name' => $this->name ?? '',
            'mobile' => $this->mobile ?? '',
            'email' => $this->email ?? '',
            'status' => $this->status ?? '',
            'status_text' => $this->status_text,
            'role_id' => $this->role_id ?? '',
            'role' => $this->role->name ?? '',
            'create_time' => $this->create_time ? $this->create_time->toDateTimeString() : ''
        ];
    }
}